package com.yangfei.mystudy1020.activity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.yangfei.mystudy1020.R;
import com.yangfei.mystudy1020.utils.Network;

import top.cuihp.serverlibrary.server.MinaServer;
import top.cuihp.serverlibrary.server.ServerConfig;

public class SocketServerActivity extends Activity {

    final String TAG = SocketServerActivity.class.getSimpleName();
    MinaServer minaServer;

    TextView tvInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_server);
        tvInfo = findViewById(R.id.tv_info);
        getPermission(this);


        String ip = Network.getLocalIp(SocketServerActivity.this);
        Log.e("SocketServerActivity","ip address = "+ip);

        findViewById(R.id.connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startServer();
            }
        });

        findViewById(R.id.disconnect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                minaServer.disConnect();
            }
        });

        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                net_num++;
                minaServer.sendMessage("msg from server no."+net_num+" time="+System.currentTimeMillis());
            }
        });
    }

    int net_num = 1;
    private void startServer() {
        //建造者模式进行相关配置
        ServerConfig serverConfig = new ServerConfig.Builder().setPort(8091).build();
        minaServer = new MinaServer(serverConfig);
        //服务器状态进行监听
        minaServer.setServerStateListener(new MinaServer.ServerStateListener() {
            @Override
            public void sessionCreated() {
                Log.e(TAG, "server sessionCreated ");
            }

            @Override
            public void sessionOpened() {
                Log.e(TAG, "server sessionOpened ");
            }

            @Override
            public void sessionClosed() {
                Log.e(TAG, "server sessionClosed ");
            }

            @Override
            public void messageReceived(String message) {
                Log.e(TAG, "server messageReceived message= "+message);
                tvInfo.setText(message);
            }

            @Override
            public void messageSent(String message) {
                Log.e(TAG, "server messageSent "+message);

            }
        });
    }

    public void getPermission(Activity act){
        if (ContextCompat.checkSelfPermission(act, Manifest.permission.ACCESS_WIFI_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(act, new String[]{Manifest.permission.ACCESS_WIFI_STATE}, 1);
        }
    }
}
